<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\DB;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Exception;

class LoginController extends Controller
{
    use ThrottlesLogins;
    
    //登录页面
    public function index(){

        
    	return view('login/login');
    }

    //修改页面
    public function member_edit(){
        $member = Db::table('admin')->where('id','=',Session::get('session_id'))->first();

        return view('member/member',['account'=>$member->account]);
    }

    //修改登录密码
    public function login_edit_pwd(Request $request){
    	$input = $request->input();
        if(empty($input['password']))throw new Exception(10001);
        
    	DB::table('admin')->where('id','=',Session::get('session_id'))->update(['pwd'=>Hash::make($input['password'])]);

        return response()->json([
            'status' => 1,
            'result' => [],
            'message' => '修改成功！'
        ]);
    }

    //登录认证
    public function login_do(Request $request){
        if ($this->hasTooManyLoginAttempts($request)) {
            return $this->sendLockoutResponse($request);
        }
    	$input = $request->input();
    	// 登录验证
		$result = DB::table('admin')->where('account','=',$input['username'])->first();
		$company = DB::table('company')->where('contact','=',$input['username'])->first();
		$son = DB::table('son')->where('name','=',$input['username'])->first();
        if(isset($result->id)){
                if(!Hash::check($input['password'], $result->pwd)){
                    return redirect('/login');
                }else {
                    if ($result->status == 0) throw new Exception("你已禁止登录!");
                    Db::table('admin')->where('account', '=', $input['username'])->update(['last_ip' => $this->get_client_ip(), 'last_time' => time()]);
                    // 用户信息存入 Session
                    \Session::put('session_id', $result->id);
                    \Session::put('username', $input['username']);
                    \Session::put('role_id', 1);
                    $admin_role_name = Db::table('admin_role')->where('role_id', '=', $result->role_id)->first();
                    \Session::put('session_role_name', $admin_role_name->role_name);
                    return redirect('/index');
                }
        }else if(isset($company->id)){
            if(Hash::check($input['password'],$company->password)) {
                // 用户信息存入 Session
                \Session::put('session_id', $company->id);
                \Session::put('company_name', $company->name);
                \Session::put('username', $input['username']);
                \Session::put('role_id', 2);
                \Session::put('session_role_name', '公司成员');
                 return redirect('/index');
            }else{
                return redirect('/login');
            }
        }else if(isset($son->id)){
                if(Hash::check($input['password'], $son->password)) {
                    \Session::put('session_id', $son->id);
                    \Session::put('role_id', 3);
                    \Session::put('username', $input['username']);
                    \Session::put('company_id', $son->company_id);
                    \Session::put('session_role_name', '子管理成员');
                   return redirect('/index');
                }else{
                    return redirect('/login');
                }
        }else{
            return redirect('/login');
        }
    }





    public function get_client_ip() {
        $ip = $_SERVER['REMOTE_ADDR'];
        if (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
            foreach ($matches[0] AS $xip) {
                if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
                    $ip = $xip;
                    break;
                }
            }
        }
        return $ip;
    }

    //退出登录
    public function login_up(){
    	\Session::forget('session_id');	

    	return redirect('/login');
    }
}
